<?php

namespace tmcore\services\addons\module;

use tmcore\services\addons\BaseService;
use tmcore\services\addons\UtilService;

class Depend extends BaseService
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取 composer.json 内容
     * @return array|mixed
     */
    public function getSysComposerContent()
    {
        return UtilService::getJsonFileContent($this->root_path . 'composer.json');
    }

    /**
     * 获取备份 composer.json 内容
     * @return array|mixed
     */
    public function getBackupSysComposerContent()
    {
        return UtilService::getJsonFileContent($this->root_path . 'data/backup/package/composer.json');
    }

    /**
     * 获取插件 composer.json 内容
     * @param $addon_key
     * @return array|mixed
     */
    public function getAddonComposerContent($addon_key)
    {
        return UtilService::getJsonFileContent($this->download_path . $addon_key . '/package/composer.json');
    }

    /**
     * 设置 composer.json 内容
     * @param $content
     * @return true
     * @throws \think\Exception
     */
    public function setSysComposerContent($content)
    {
        return UtilService::writeJsonFile($content,$this->root_path . 'composer.json');
    }

    /**
     * 获取 npm package.json 内容
     * @param $type
     * @return array|mixed
     */
    public function getSysNpmContent($type)
    {
        if(!in_array($type,['admin','uniapp','pc'])) return [];
        return UtilService::getJsonFileContent($this->sys_path . $type  . '/package.json');
    }

    /**
     * 获取备份 npm package.json 内容
     * @param $type
     * @return array|mixed
     */
    public function getBackupSysNpmContent($type)
    {
        if(!in_array($type,['admin','uniapp','pc'])) return [];
        return UtilService::getJsonFileContent($this->root_path . 'data/backup/package/' . $type  . '_package.json');
    }

    /**
     * 获取插件 npm package.json 内容
     * @param $addon_key
     * @param $type
     * @return array|mixed
     */
    public function getAddonNpmContent($addon_key, $type)
    {
        if(!in_array($type,['admin','uniapp','pc'])) return [];
        return UtilService::getJsonFileContent($this->download_path . $addon_key . '/package/' . $type . '_package.json');
    }

    /**
     * 设置 npm package.json 内容
     * @param $content
     * @param $type
     * @return true
     * @throws \think\Exception
     */
    public function setSysNpmContent($content, $type)
    {
        return UtilService::writeJsonFile($content,$this->sys_path . $type  . '/package.json');
    }


    /**
     * 获取 uniapp pages.json 内容
     * @return array|mixed
     */
    public function getSysPageContent()
    {
        return UtilService::getJsonFileContent($this->sys_path . 'uniapp/src/pages.json');
    }

    /**
     * 获取备份 uniapp pages.json 内容
     * @return array|mixed
     */
    public function getBackupSysPageContent()
    {
        return UtilService::getJsonFileContent($this->root_path . 'data/backup/package/uniapp_pages.json');
    }

    /**
     * 获取插件 uniapp pages.json 内容
     * @param $addon_key
     * @return array|mixed
     */
    public function getAddonPageContent($addon_key)
    {
        return UtilService::getJsonFileContent($this->download_path . $addon_key . '/package/uniapp_pages.json');
    }

    /**
     * 设置 uniapp pages.json 内容
     * @param $content
     * @return true
     * @throws \think\Exception
     */
    public function setSysPageContent($content)
    {
        return UtilService::writeJsonFile($content,$this->sys_path . 'uniapp/src/pages.json');
    }

    /**
     * 检查依赖是否可以删除
     * @param string $depend_item 依赖
     * @param array $addon_depend_list 插件依赖列表
     * @param array $reserve_depend_list 保留依赖列表
     * @return bool
     */
    public function checkAddonDelDepend($depend_item, $addon_depend_list, $reserve_depend_list = [])
    {
        // 检验依赖是否匹配插件
        if(!array_key_exists($depend_item, $addon_depend_list)) return false;

        // 检验依赖是否包含在保留的依赖项
        if($reserve_depend_list && array_key_exists($depend_item, $reserve_depend_list)) return false;

        return true;
    }

    /**
     * 检查页面是否可以删除
     * @param $path
     * @param $backup_pages_list
     * @return mixed|true 返回原始配置
     */
    public function checkAddonDelPages($path, $backup_pages_list = []) {
        foreach ($backup_pages_list as $key => $backup_item) {
            // 寻找原始路径匹配
            if($path && $path == $backup_item['path']) {
                return $backup_item;
            }
        }

        return true;
    }
}